"""
_*_ : utf-8 _*_ 
@Time: 2023/10/31 20:56
@Author: LWK
@File: 059_urllib_post请求百度翻译
@Project: demo01
"""
import json
import urllib.request
import urllib.parse

url = 'https://fanyi.baidu.com/sug'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76'
}

param = {
    'kw': 'dog'
}

# post请求的参数，必须要进行编码!!!
data = urllib.parse.urlencode(param).encode('utf-8')

# post的请求参数，是不会拼接在url后面的，而是需要放在请求对象定制的参数中
req = urllib.request.Request(url, data, headers)

# 模拟浏览器向服务器发送请求
res = urllib.request.urlopen(req)

# 获取响应的数据
content = res.read().decode('utf-8')

obj = json.loads(content)

print(obj)
